package com.leoman.dao;

import com.leoman.entity.FeedBack;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 意见反馈
 * Created by Administrator on 2016/10/18 0018 下午 3:02.
 */
public interface FeedBackDao extends JpaRepository<FeedBack, Integer>, JpaSpecificationExecutor<FeedBack> {

    @Query("select a from FeedBack a where a.sendUserId in (select b.id from AppUser b where b.nickName like ?1 and a.sendType = 1) or a.sendUserId in (select c.id from CompanyUser c where c.nickName like ?1 and a.sendType = 2)")
    Page<FeedBack> pageOnlyWithNickName(String nickName, Pageable pageable);

    @Query("select a from FeedBack a where a.mobile like ?2 and (a.sendUserId in (select b.id from AppUser b where b.nickName like ?1 and a.sendType = 1) or a.sendUserId in (select c.id from CompanyUser c where c.nickName like ?1 and a.sendType = 2))")
    Page<FeedBack> pageWithNickNameAndMobile(String nickName, String mobile, Pageable pageable);

    @Query("select a from FeedBack a where a.sendType = ?2 and (a.sendUserId in (select b.id from AppUser b where b.nickName like ?1 and a.sendType = 1) or a.sendUserId in (select c.id from CompanyUser c where c.nickName like ?1 and a.sendType = 2))")
    Page<FeedBack> pageWithNickNameAndSendType(String nickName, Integer sendType, Pageable pageable);

    @Query("select a from FeedBack a where a.sendType = ?3 and a.mobile like ?2 and (a.sendUserId in (select b.id from AppUser b where b.nickName like ?1 and a.sendType = 1) or a.sendUserId in (select c.id from CompanyUser c where c.nickName like ?1 and a.sendType = 2))")
    Page<FeedBack> pageWithNickNameAndMobileAndSendType(String nickName, String mobile, Integer sendType, Pageable pageable);

    @Query("select a from FeedBack a where a.mobile like ?1")
    Page<FeedBack> pageOnlyWithMobile(String mobile, Pageable pageable);

    @Query("select a from FeedBack a where a.mobile like ?1 and a.sendType = ?2")
    Page<FeedBack> pageWithMobileAndSendType(String mobile, Integer sendType, Pageable pageable);

    @Query("select a from FeedBack a where a.sendType = ?1")
    Page<FeedBack> pageOnlyWithSendType(Integer sendType, Pageable pageable);

    @Query("select a from FeedBack a where a.createTime >= ?1")
    List<FeedBack> findListByCreateTime(Long createTime);
}